package i5.modelman.merge.dependency;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import i5.modelman.chase.PrologUtil;
import i5.modelman.chase.ParsingException;
import jpl.Query;
import jpl.Util;

public class ConstraintFactory {
	
	
	public static String patternString = ImplicationDirection.BIDIRECTIONAL.text + "|"
	+ ImplicationDirection.LEFT_TO_RIGHT.text + "|" 
	+ ImplicationDirection.RIGHT_TO_LEFT.text;
	public static Pattern pattern = Pattern.compile(patternString);
	
//	public static Constraint createFromString(String text) throws ParsingException{
//		return ContextualConstraintFactory.createFromString(text, null);
//		//		Matcher matcher = pattern.matcher(text);
////		if(matcher.find()){
////			String found = matcher.group();
////			ImplicationDirection direction = ImplicationDirection.sign2Direction(found);
////			Query leftQuery = new Query(Util.textToTerm(text.substring(0, matcher.start())));
////			Query rightQuery = new Query(Util.textToTerm(text.substring(matcher.end())));
////			return new ConstraintImpl(leftQuery, rightQuery, direction);
////		}
////		throw new ParsingException("Invalid Syntax for Constraint:\r\n" + text);
//	}
	
	

	public static Constraint createConstraint(Query leftQuery, Query rightQuery,
			ImplicationDirection direction){
		return new ConstraintImpl(leftQuery, rightQuery, direction);
	}
	
//	public static InclusionDependency create(String srcPred, String[] srcAttrs, String destPred, String[] destAttrs, Model model) throws AttributeNotFoundException{
//		int destArity = MergeUtil.getArityWithinModels(destPred, model);
//		int srcArity = MergeUtil.getArityWithinModels(srcPred, model);
//		
//		ArrayList<int[]> pairs = new ArrayList<int[]>();
//		for(int i = 0; i < srcAttrs.length; i++){
//			int destIndex = MergeUtil.getIndexWithinPredicate(destPred, destAttrs[i].trim(), model);
//			int srcIndex = MergeUtil.getIndexWithinPredicate(srcPred, srcAttrs[i].trim(), model);
//			pairs.add(new int[]{destIndex, srcIndex});
//		}
//		
//		return new InclusionDependency(new String[]{destPred, srcPred}, new int[]{destArity, srcArity}, pairs);
//	}
//	
//	public static KeyDependency create(String predicate, Collection<String> primaryAttrs, Model model) throws AttributeNotFoundException{
//		int arity = MergeUtil.getArityWithinModels(predicate, model);
////			PrologUtil.getArityOf(predicate);
//		Set<Integer> key = new HashSet<Integer>();
//		for(String attr : primaryAttrs){
//			int index = MergeUtil.getIndexWithinPredicate(predicate, attr, model);
//			key.add(index);
//		}
//		if(arity <= key.size()){
//			throw new AttributeNotFoundException("Unable to find any non-primary attributes!");
//		}
//		return new KeyDependency(predicate, arity, key);
//	}
}
